using System.Runtime.CompilerServices;
using System.Collections.Generic;

//
// * Copyright (c) 2002-2009 "Neo Technology,"
// *     Network Engine for Objects in Lund AB [http://neotechnology.com]
// *
// * This file is part of Neo4j.
// * 
// * Neo4j is free software: you can redistribute it and/or modify
// * it under the terms of the GNU Affero General Public License as
// * published by the Free Software Foundation, either version 3 of the
// * License, or (at your option) any later version.
// * 
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// * GNU Affero General Public License for more details.
// * 
// * You should have received a copy of the GNU Affero General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// 
namespace org.neo4j.kernel.impl.util
{


	public class ArrayMap<K, V>
	{
		private ArrayEntry<K, V>[] arrayEntries;

		private volatile int arrayCount = 0;
		private int toMapThreshold = 5;
		private Map<K, V> propertyMap = null;
		private readonly bool useThreadSafeMap;
		private bool switchBackToArray = false;

		public ArrayMap()
		{
			useThreadSafeMap = false;
			arrayEntries = new ArrayEntry[toMapThreshold];
		}

		public ArrayMap(int mapThreshold, bool threadSafe, bool shrinkToArray)
		{
			this.toMapThreshold = mapThreshold;
			this.useThreadSafeMap = threadSafe;
			this.switchBackToArray = shrinkToArray;
			arrayEntries = new ArrayEntry[toMapThreshold];
		}

		public virtual void put(K key, V @value)
		{
			if (useThreadSafeMap)
			{
				synchronizedPut(key, @value);
				return;
			}
			for (int i = 0; i < arrayCount; i++)
			{
				if (arrayEntries[i].Key.Equals(key))
				{
					arrayEntries[i].setNewValue(@value);
					return;
				}
			}
			if (arrayCount != -1)
			{
				if (arrayCount < arrayEntries.Length)
				{
					arrayEntries[arrayCount++] = new ArrayEntry<K, V>(key, @value);
				}
				else
				{
					propertyMap = new Dictionary<K, V>(arrayEntries.Length * 2);
					for (int i = 0; i < arrayCount; i++)
					{
						propertyMap.put(arrayEntries[i].Key, arrayEntries[i].Value);
					}
					arrayCount = -1;
					propertyMap.put(key, @value);
				}
			}
			else
			{
				propertyMap.put(key, @value);
			}
		}

		[MethodImpl(MethodImplOptions.Synchronized)]
		private void synchronizedPut(K key, V @value)
		{
			for (int i = 0; i < arrayCount; i++)
			{
				if (arrayEntries[i].Key.Equals(key))
				{
					arrayEntries[i].setNewValue(@value);
					return;
				}
			}
			if (arrayCount != -1)
			{
				if (arrayCount < arrayEntries.Length)
				{
					arrayEntries[arrayCount++] = new ArrayEntry<K, V>(key, @value);
				}
				else
				{
					propertyMap = new Dictionary<K, V>(arrayEntries.Length * 2);
					for (int i = 0; i < arrayCount; i++)
					{
						propertyMap.put(arrayEntries[i].Key, arrayEntries[i].Value);
					}
					arrayEntries = null;
					arrayCount = -1;
					propertyMap.put(key, @value);
				}
			}
			else
			{
				propertyMap.put(key, @value);
			}
		}

		public virtual V @get(K key)
		{
			if (key == null)
			{
				return null;
			}
			if (useThreadSafeMap)
			{
				return synchronizedGet(key);
			}
			int count = arrayCount;
			for (int i = 0; i < count; i++)
			{
				if (key.Equals(arrayEntries[i].Key))
				{
					return arrayEntries[i].Value;
				}
			}
			if (arrayCount == -1)
			{
				return propertyMap.get(key);
			}
			return null;
		}

		[MethodImpl(MethodImplOptions.Synchronized)]
		private V synchronizedGet(K key)
		{
			for (int i = 0; i < arrayCount; i++)
			{
				if (key.Equals(arrayEntries[i].Key))
				{
					return arrayEntries[i].Value;
				}
			}
			if (arrayCount == -1)
			{
				return propertyMap.get(key);
			}
			return null;
		}

		[MethodImpl(MethodImplOptions.Synchronized)]
		private V synchronizedRemove(K key)
		{
			for (int i = 0; i < arrayCount; i++)
			{
				if (arrayEntries[i].Key.Equals(key))
				{
					V removedProperty = arrayEntries[i].Value;
					arrayCount--;
					System.Array.Copy(arrayEntries, i + 1, arrayEntries, i, arrayCount - i);
					return removedProperty;
				}
			}
			if (arrayCount == -1)
			{
				V @value = propertyMap.Remove(key);
				if (switchBackToArray && propertyMap.size() < toMapThreshold)
				{
					arrayEntries = new ArrayEntry[toMapThreshold];
					int tmpCount = 0;
					foreach (KeyValuePair<K, V> entry in propertyMap.entrySet())
					{
						arrayEntries[tmpCount++] = new ArrayEntry<K, V>(entry.Key, entry.Value);
					}
					arrayCount = tmpCount;
				}
				return @value;
			}
			return null;
		}

		public virtual V remove(K key)
		{
			if (useThreadSafeMap)
			{
				return synchronizedRemove(key);
			}
			for (int i = 0; i < arrayCount; i++)
			{
				if (arrayEntries[i].Key.Equals(key))
				{
					V removedProperty = arrayEntries[i].Value;
					arrayCount--;
					System.Array.Copy(arrayEntries, i + 1, arrayEntries, i, arrayCount - i);
					return removedProperty;
				}
			}
			if (arrayCount == -1)
			{
				V @value = propertyMap.Remove(key);
				if (switchBackToArray && propertyMap.size() < toMapThreshold)
				{
					arrayEntries = new ArrayEntry[toMapThreshold];
					int tmpCount = 0;
					foreach (KeyValuePair<K, V> entry in propertyMap.entrySet())
					{
						arrayEntries[tmpCount++] = new ArrayEntry<K, V>(entry.Key, entry.Value);
					}
					arrayCount = tmpCount;
				}
				return @value;
			}
			return null;
		}

		internal class ArrayEntry<K, V> : Entry<K, V>
		{
			private K key;
			private V @value;

			internal ArrayEntry(K key, V @value)
			{
				this.key = key;
				this.value = @value;
			}

			public virtual K Key
			{
				get
				{
					return key;
				}
			}

			public virtual V Value
			{
				get
				{
					return @value;
				}
				set
				{
					V oldValue = value;
					this.value = value;
					return oldValue;
				}
			}

			internal virtual void setNewValue(V @value)
			{
				this.value = @value;
			}

		}

		public virtual IEnumerable <K> keySet()
		{
			if (arrayCount == -1)
			{
				return propertyMap.Keys;
			}
			List<K> keys = new LinkedList<K>();
			for (int i = 0; i < arrayCount; i++)
			{
				keys.Add(arrayEntries[i].Key);
			}
			return keys;
		}

		public virtual IEnumerable <V> values()
		{
			if (arrayCount == -1)
			{
				return propertyMap.values();
			}
			List<V> values = new LinkedList<V>();
			for (int i = 0; i < arrayCount; i++)
			{
				values.Add(arrayEntries[i].Value);
			}
			return values;
		}

		public virtual Set<KeyValuePair<K, V>> entrySet()
		{
			if (arrayCount == -1)
			{
				return propertyMap.entrySet();
			}
			Set<KeyValuePair<K, V>> entries = new HashSet<KeyValuePair<K, V>>();
			for (int i = 0; i < arrayCount; i++)
			{
				entries.Add(arrayEntries[i]);
			}
			return entries;
		}

		public virtual int size()
		{
			if (arrayCount != -1)
			{
				return arrayCount;
			}
			return propertyMap.size();
		}

		public virtual void clear()
		{
			if (arrayCount != -1)
			{
				arrayCount = 0;
			}
			else
			{
				propertyMap.Clear();
			}
		}
	}
}